package com.zlp.boot1;

import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

public class Day20210602 {


    @Test
    public void tt(){
        String str;
        System.out.println(checkSubarraySum(new int[]{2, 4}, 6));
    }
    public boolean checkSubarraySum(int[] nums, int k) {
        //使用了同余定理和前缀和+哈希的
        Map<Integer,Integer> map = new HashMap<>();
        map.put(0,-1);
        int sum = 0;
        for (int i=0;i<nums.length;i++){
            sum += nums[i];
            //把余数放入map中；
            int value = sum % k;
            if(map.containsKey(value)){
                if(i-map.get(value)>1) return true;
            }else {
                map.put(value,i);
            }
        }
        return false;
    }
}

